package hasthi.actuators.impl;

import hasthi.actuators.ActionCenter;
import hasthi.actuators.ActionContext;
import hasthi.actuators.ManagementAction;
import hasthi.actuators.actions.ActionCallback;
import hasthi.common.HasthiException;
import hasthi.common.HasthiException.FaultCode;
import hasthi.decision.Enviorment;
import hasthi.metamodel.resource.ManagedResource;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

public class LoggingActuatorBase implements ActionCenter {
    private List<String> overloadedServices = new Vector<String>();
    private List<String> overloadedServiceHosts = new Vector<String>();
    
    private FileWriter actionLog;
    private Map<String,Object> map = new ConcurrentHashMap<String, Object>();
    
    public void init(ActionContext actionContext) throws HasthiException {
        try {
            actionLog = new FileWriter("action.log",true);
        } catch (IOException e) {
            throw new HasthiException(e,FaultCode.InitalizationError);
        }
        
    }
    
    public void append(String val){
        try {
            actionLog.append(val).append("\n");
            actionLog.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void signal(String signal, String... properties) {
        append("Signal:"+signal + Arrays.toString(properties));
    }

    public void setProperty(ManagedResource resource, String name, String value)
            throws HasthiException {
        append("Action:Set Property "+resource.getName()+"@"+name+"="+value);
    }

    public void setPlanning(boolean planning) {
    }

    public void planAndExecuate() throws HasthiException {
    }

    public void invoke(ManagementAction action) throws HasthiException {
        append("Action:"+action.getActionID());
    }
    
    public void invoke(ManagementAction action, ActionCallback callback) throws HasthiException {
        append("Action:"+action.getActionID());
    }

    public void invoke(ManagedResource resource, String operation, String soapAcion,
            String... parameters) throws HasthiException {
        append("Action:Called "+operation + Arrays.toString(parameters));
    }

    public List<String> getOverloadedServices() {
        return overloadedServices;
    }

    public List<String> getOverloadedServiceHosts() {
        return overloadedServiceHosts;
    }

    public ActionContext getActionContext() {
        return null;
    }

    public void createService(String serviceName) throws HasthiException {
        append("Action:Create Service "+ serviceName);
    }

    public Enviorment getEnviorment() {
        // TODO Auto-generated method stub
        return null;
    }
    public Object put(String key, Object value) {
        return map.put(key, value);
    }

    public Object get(String key) {
        return map.get(key);
    }

    public void remove(String key) {
        map.remove(key);
    }
}
